//
//  SHYRouter.m
//  Pods
//
//  Created by sunhaiyan on 16/6/17.
//
//

#import "SHYRouter.h"
#import "SHYSchemaManager+Private.h"
#import "SHYSchemaManager.h"

@implementation SHYRouter

+ (instancetype)sharedInstance {
    static dispatch_once_t onceInstance;
    static SHYRouter *router;
    dispatch_once(&onceInstance, ^{
        router = [[SHYRouter alloc] init];
    });
    return router;
}

- (BOOL)canOpenUrl:(NSURL *)url {
    return [[SHYSchemaManager defaultManager] canOpenUrl:url];
}


- (void)pushToUrl:(NSURL *)url {
    if (![self canOpenUrl:url]) {
        return;
    }
    [self setSelectTabControllerWithUrl:url];

    UIViewController * controller = [[SHYSchemaManager defaultManager] controllerForUrl:url];

    __block UIViewController *popToViewController = nil;
    [self.rootViewController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:controller.class]) {
            popToViewController = obj;
            *stop = YES;
        }
    }];
    
    if (popToViewController) {
        NSLog(@"堆栈里面已经有该控制器了，修改控制器的ViewControllers保证只能存在一份");
        NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.rootViewController.viewControllers];
        [controllers removeObject:popToViewController];
        [self.topViewController.navigationController setViewControllers:controllers animated:NO];
        [self.topViewController.navigationController pushViewController:controller animated:YES];
    }
    else {
        [self.topViewController.navigationController pushViewController:controller animated:YES];
    }
}

- (void)popToUrl:(NSURL *)url {
    if (![self canOpenUrl:url]) {
        return;
    }
    [self setSelectTabControllerWithUrl:url];
    
    UIViewController * controller = [[SHYSchemaManager defaultManager] controllerForUrl:url];
    __block UIViewController *popToViewController = nil;
    
    [self.rootViewController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[controller class]]) {
            popToViewController = obj;
            *stop = YES;
        }
    }];
    if (popToViewController) {
        [self.topViewController.navigationController popToViewController:popToViewController animated:YES];
    }
    else {
        NSLog(@"队列中没有改控制器，无法pop");
    }
}

- (void)setSelectTabControllerWithUrl:(NSURL *)url {
    if (!self.tabBarController) {
        return;
    }
    NSInteger index = [[SHYSchemaManager defaultManager] indexForSelectTabViewController:url];
    [self.tabBarController setSelectedIndex:index];
}

- (UIViewController *)topViewController {
   return self.rootViewController.topViewController;
}

- (UINavigationController *)rootViewController {
    if (self.tabBarController) {
        if ([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]]) {
            return (UINavigationController *)self.tabBarController.selectedViewController;
        }
    }
    return _rootViewController;
}

@end
